<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class master extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		//$this->output->enable_profiler(true);
		$this->load->model('master_model');
	}
	
	function index(){
		$this->load->view('master_view');
	}
	
	function grid(){
		$data = $this->master_model->getdata();
		echo $this->master_model->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['ID_MASTER'] = '';
		$data['TGROUP'] = '';
		$data['CODE'] = '';
		$data['NAME'] = '';
		$data['DESCRIPTION'] = '';
	
		$this->load->view('master_form_view', $data);
	}
	
	/* function edit($kode){
		$r = $this->mdl_prodi->getdataedit($kode);
		
		$data['kode'] = $kode;
		$data['kd_pt'] = $r->row()->kd_pt;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_program'] = $r->row()->kd_program;
		$data['kd_jenjang'] = $r->row()->kd_jenjang;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['nama_prodi'] = $r->row()->nama_prodi;
		$data['status'] = $r->row()->status;
		
		$this->load->view('prodi_form', $data);
	} */
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		
		# rules validasi form
		$this->form_validation->set_rules("CODE", 'Code', 'trim|required|xss_clean');
		$this->form_validation->set_rules("NAME", 'Nama', 'trim|required|xss_clean');
		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_prodi',' ',' '))==''?'':form_error('kd_prodi',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi=="add"){ // add
				//$result = !$this->mdl_prodi->isExistKode($data['kd_prodi']);
				//if ($result){
					$result = $this->master_model->InsertOnDb($data);
				//}else{
				//	$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
				//}
			}else { // edit
				if($data['kode'] == $data['kd_prodi']){ // jika kode tidak diubah
					$result=$this->mdl_prodi->UpdateOnDb($data);
				}else{
					$result = !$this->mdl_prodi->isExistKode($data['kd_prodi']);
					if ($result){
						$result=$this->mdl_prodi->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_prodi->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
	function detail($kode){
		$r = $this->mdl_proposal->getdatadetail($kode);
		
		//$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
		//s$name = 'myphoto.jpg';

		//force_download($name, $data);
		
		$data['id'] = $kode;
		$data['tahun_id'] = $r->row()->tahun_id;
		$data['nim'] = $r->row()->nim;
		$data['nama'] = $r->row()->nama;
		$data['judul_proposal'] = $r->row()->judul_proposal;
		$data['file_proposal'] = $r->row()->file_proposal;
		$data['kd_fakultas'] = $r->row()->kd_fakultas;
		$data['kd_prodi'] = $r->row()->kd_prodi;
		$data['pembimbing1'] = $r->row()->pembimbing1;
		$data['pembimbing2'] = $r->row()->pembimbing2;
		$data['proposal'] = $r->row()->proposal;
		$data['keterangan'] = $r->row()->keterangan;
		$data['tgl_daftar'] = FormatDateFromMysql($r->row()->tgl_daftar);
		$data['aksi'] = 'detail';
		$this->load->view('/ta/proposal_detail', $data);
	}
	function grid_detail(){
		$data = $this->master_model->getdata();
		echo $this->master_model->togrid($data['row_data'], $data['row_count']);
	}
}